package com.sky.controller.user;

import com.sky.controller.BaseController;
import com.sky.enumeration.ShopStatusEnum;
import com.sky.result.Result;
import com.sky.utils.RedisCacheUtils;
import com.sky.utils.TransformUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userShopController")
@RequestMapping("/user/shop")
@Api(tags = "店铺操作接口")
@Slf4j
public class ShopController extends BaseController {

    @Autowired
    private RedisCacheUtils redisCacheUtils;

    @PutMapping("/{status}")
    @ApiOperation("更新店铺营业状态")
    public Result put(@PathVariable String status){
        ShopStatusEnum statusEnum = TransformUtils.transformEnum(status, ShopStatusEnum.class);
        assert statusEnum != null; // 断言，如果断言不成立，则抛出错误
        log.info("设置营业状态 {}",statusEnum.getDesc());
        redisCacheUtils.setCacheObject("SHOP_STATUS",statusEnum.getCode());
        return Result.success();
    }

    @GetMapping("/status")
    @ApiOperation("获取店铺营业状态")
    public Result get(){
        Integer shopStatus =  (Integer)redisCacheUtils.getCacheObject("SHOP_STATUS");
        if(shopStatus == null){
            shopStatus = ShopStatusEnum.Disable.getCode();
        }
        log.info("获取店铺营业状态 {}",shopStatus);
        return Result.success(shopStatus);
    }
}
